<template>
  <div class="app-container">
    <el-card style="margin-bottom: 10px">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="课程名称" prop="courseName">
          <el-input
            v-model="queryParams.courseName"
            placeholder="请输入课程名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="课程类别" prop="courseType">
          <el-select
            v-model="queryParams.courseType"
            placeholder="请选择课程类别"
            clearable
          >
            <el-option
              v-for="dict in dict.type.ocss_course_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-thumb"
            size="small"
            @click="openDrawer"
            >查看已选课程</el-button
          >
        </el-col>

        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <!-- 可选课程列表 -->
      <el-table v-loading="loading" :data="courseList">
        <el-table-column label="课程名称" align="center" prop="courseName" />
        <el-table-column label="学分" width="50" align="center" prop="credit" />
        <el-table-column label="课程类别" align="center" prop="courseType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.ocss_course_type"
              :value="scope.row.courseType"
            />
          </template>
        </el-table-column>
        <el-table-column label="选课情况" align="center" prop="courseNumber">
          <template slot-scope="scope">
            <el-tag effect="dark" type="success"
              >课程数量：{{ scope.row.courseNumber }}</el-tag
            >
            <el-tag effect="dark" style="margin-top: 5px" type="warning"
              >剩余数量：{{ scope.row.remainingCourses }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="上课时段" align="center" prop="classTimer" />
        <el-table-column label="上课地点" align="center" prop="classLocation" />

        <el-table-column
          label="抢课结束时间"
          align="center"
          prop="courseEndTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.courseEndTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-circle-check"
              @click="handleSelect(scope.row)"
              v-hasPermi="['system:course_selection:select']"
              >抢课</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="handleDesc(scope.row)"
              >课程介绍</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 已选课程列表 -->
    <el-drawer title="当前已选课程" size="60%" :visible.sync="drawer">
      <el-table
        v-loading="selectLoading"
        v-if="selectedCourseList.length !== 0"
        :data="selectedCourseList"
      >
        <el-table-column label="课程名称" align="center" prop="courseName" />
        <el-table-column label="学分" width="50" align="center" prop="credit" />
        <el-table-column label="课程类别" align="center" prop="courseType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.ocss_course_type"
              :value="scope.row.courseType"
            />
          </template>
        </el-table-column>
        <el-table-column label="课程人数" align="center" prop="courseNumber" />
        <el-table-column label="上课时段" align="center" prop="classTimer" />
        <el-table-column label="上课地点" align="center" prop="classLocation" />

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-circle-close"
              @click="handleRetreat(scope.row)"
              v-hasPermi="['system:course_selection:retreat']"
              >退课</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="selectTotal > 0"
        :total="selectTotal"
        :page.sync="selectedQueryParams.pageNum"
        :limit.sync="selectedQueryParams.pageSize"
        @pagination="getSelectedCourseList"
      />
      <span v-if="selectedCourseList.length == 0">当前还未选择任何课程</span>
    </el-drawer>

    <!-- 查看课程介绍对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <p class="course_desc">
        {{ form.description }}
      </p>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCourseSelection,
  robClasses,
  listSelectedCourseList,
  retreat,
} from "@/api/system/course_selection";
import { getCourse } from "@/api/system/course";

export default {
  name: "Course",
  dicts: ["ocss_course_type", "ocss_course_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      selectLoading: false,

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程信息表格数据
      courseList: [],
      selectedCourseList: [],
      // 弹出层标题
      title: "",
      form: {},
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        courseType: null,
      },
      // 抽屉是否打开
      drawer: false,
      selectTotal: 0,
      selectedQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询课程信息列表 */
    getList() {
      this.loading = true;
      listCourseSelection(this.queryParams).then((response) => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询已选课程列表
    getSelectedCourseList() {
      this.selectLoading = true;
      listSelectedCourseList().then((res) => {
        this.selectedCourseList = res.rows;
        this.selectTotal = res.total;
        this.selectLoading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    openDrawer() {
      this.drawer = true;
      this.getSelectedCourseList();
    },

    // 抢课操作
    handleSelect(row) {
      robClasses(row).then((res) => {
        if (res.code == 200) {
          this.getList();
          this.$message.success(res.msg);
        }
      });
    },

    // 退课操作
    handleRetreat(row) {
      retreat(row).then((res) => {
        if (res.code == 200) {
          this.$message.success(res.msg);
          this.getList();
          this.getSelectedCourseList();
        }
      });
    },

    /** 查看课程介绍 */
    handleDesc(row) {
      getCourse(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "课程介绍信息";
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.course_desc {
  font-size: 16px;
  line-height: 30px;
  letter-spacing: 1px;
}
.el-drawer {
  span {
    font-size: 15px;
    color: #535353;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}
</style>
